<?php

namespace App\Jobs\Controller;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalConst;
use App\Jobs\BaseJob;
use App\Models\UserMessageLikeReceive;
use App\Models\UserMessageLikeSend;

class LikeJob extends BaseJob
{
    public function cancelLikeByNote($msgContent)
    {
        $targetId = $msgContent['msgContent']['target_id'];
        $receiveId = $msgContent['msgContent']['user_id'];

        $likeModel = UserMessageLikeReceive::getModel($receiveId);
        $msgLikeReceives = $likeModel
            ->where('receive_id', $receiveId)
            ->where('target_id', $targetId)
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
            ->get();

        $senderIdArr = ArrayHelper::getColumn($msgLikeReceives, 'sender_id');
        ArrayHelper::removeValue($senderIdArr, $receiveId);

        foreach ($senderIdArr as $senderId) {
            $msgLikeSend = UserMessageLikeSend::getModel($receiveId)
                ->where('sender_id', $senderId)
                ->where('target_id', $targetId)
                ->first();
            if ($msgLikeSend) {
                $msgLikeSend->update(['is_deleted' => GlobalConst::IS_DELETED]);
            }
        }

        $likeModel->where('receive_id', $receiveId)
            ->where('target_id', $targetId)
            ->update(['is_deleted' => GlobalConst::IS_DELETED]);

        return true;
    }
}
